home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / opbonus.arc / DUPFIND.ARC / OPUNIQUE.PAS < prev    next >
Pascal/Delphi Source File  |  1991-03-20  |  6KB  |  214 lines

  1. {$S-,R-,V-,I-,B-,F+,O-,A-}
  2.  
  3. {Conditional defines that may affect this unit}
  4. {$I OPDEFINE.INC}
  5.  
  6. {*********************************************************}
  7. {*                  OPUNIQUE.PAS 1.03                    *}
  8. {*      Copyright (c) TurboPower Software 1987,1989.     *}
  9. {*                 All rights reserved.                  *}
  10. {*********************************************************}
  11.  
  12. unit OpUnique;
  13.   {-String array that stores only unique copies of strings}
  14.  
  15. interface
  16.  
  17. uses
  18.   OpString, OpRoot, OpTree;
  19.  
  20. type
  21.   IndexTreeNodePtr = ^IndexTreeNode;
  22.   IndexTreeNode =
  23.     object(TreeNode)
  24.       itnIndex : Word;  {Index into StringArray for the string}
  25.  
  26.       constructor Init(Index : Word);
  27.         {-Set initial index}
  28.     end;
  29.  
  30.   IndexTreePtr = ^IndexTree;
  31.   IndexTree =
  32.     object(Tree)
  33.       itSP : StringArrayPtr;
  34.  
  35.       constructor Init(SAP : StringArrayPtr);
  36.         {-Initialize and connect to StringArray}
  37.       function Compare(Key1, Key2 : Pointer) : CompareType; virtual;
  38.         {-Compare two keys, returning Less, Equal, Greater}
  39.       function GetKey(N : TreeNodePtr) : Pointer; virtual;
  40.         {-Return a pointer to the key value for node N}
  41.     end;
  42.  
  43.   UniqueStringArrayPtr = ^UniqueStringArray;
  44.   UniqueStringArray =
  45.     object(StringArray)
  46.       usTP : IndexTreePtr; {Pointer to IndexTree}
  47.  
  48.       constructor Init(StrMax, Amount : Word);
  49.         {-Allocate space for StrMax strings in Amount space}
  50.       destructor Done; virtual;
  51.         {-Deallocate array}
  52.       function AddString(St : String) : Word; virtual;
  53.         {-Add a new string, returning its index, or 0 if error}
  54.       procedure RemoveString(Which : Word); virtual;
  55.         {-Remove specified string from array and pack character table}
  56.       procedure RemoveStringByName(St : String);
  57.         {-Remove named string}
  58.       procedure Clear;
  59.         {-Remove all strings from array}
  60.       function GetTreePtr : IndexTreePtr;
  61.         {-Return address of associated IndexTree}
  62.  
  63.     {$IFDEF UseStreams}
  64.       constructor Load(var S : IdStream);
  65.         {-Load a binary packed array from a stream. NOT SUPPORTED}
  66.       procedure Store(var S : IdStream);
  67.         {-Write a packed array to a stream. NOT SUPPORTED}
  68.     {$ENDIF}
  69.     end;
  70.  
  71.   {====================================================================}
  72.  
  73. implementation
  74.  
  75.   constructor IndexTreeNode.Init(Index : Word);
  76.     {-Set initial index}
  77.   begin
  78.     if not TreeNode.Init then
  79.       Fail;
  80.     itnIndex := Index;
  81.   end;
  82.  
  83.   {--------------------------------------------------------------------}
  84.  
  85.   constructor IndexTree.Init(SAP : StringArrayPtr);
  86.     {-Initialize and connect to StringArray}
  87.   begin
  88.     if not Tree.Init then
  89.       Fail;
  90.     itSP := SAP;
  91.   end;
  92.  
  93.   function IndexTree.Compare(Key1, Key2 : Pointer) : CompareType;
  94.     {-Compare two keys, returning Less, Equal, Greater}
  95.   begin
  96.     Compare := CompString(StringPtr(Key1)^, StringPtr(Key2)^);
  97.   end;
  98.  
  99.   function IndexTree.GetKey(N : TreeNodePtr) : Pointer;
  100.     {-Return a pointer to the key value for node N}
  101.   begin
  102.     GetKey := itSP^.GetStringPtr(IndexTreeNodePtr(N)^.itnIndex);
  103.   end;
  104.  
  105.   {--------------------------------------------------------------------}
  106.  
  107.   constructor UniqueStringArray.Init(StrMax, Amount : Word);
  108.     {-Allocate space for StrMax strings in Amount space}
  109.   begin
  110.     usTP := nil;
  111.  
  112.     if not StringArray.Init(StrMax, Amount) then
  113.       Fail;
  114.  
  115.     new(usTP, Init(@Self));
  116.     if usTP = nil then begin
  117.       Done;
  118.       InitStatus := epFatal+ecOutOfMemory;
  119.       Fail;
  120.     end;
  121.   end;
  122.  
  123.   destructor UniqueStringArray.Done;
  124.     {-Deallocate array}
  125.   begin
  126.     if usTP <> nil then
  127.       Dispose(usTP, Done);
  128.     StringArray.Done;
  129.   end;
  130.  
  131.   function UniqueStringArray.AddString(St : String) : Word;
  132.     {-Add a new string, returning its index, or 0 if error}
  133.   var
  134.     ITNP : IndexTreeNodePtr;
  135.     Index : Word;
  136.   begin
  137.     ITNP :=IndexTreeNodePtr(usTP^.Find(@St));
  138.     if ITNP = nil then begin
  139.       {String is not in the array, add it now}
  140.       Index := StringArray.AddString(St);
  141.       if Index = 0 then begin
  142.         AddString := 0;
  143.         Exit;
  144.       end;
  145.       {Create new tree node for the string}
  146.       new(ITNP, Init(Index));
  147.       if ITNP = nil then begin
  148.         AddString := 0;
  149.         Exit;
  150.       end;
  151.       {Insert it into binary tree}
  152.       usTP^.Insert(ITNP);
  153.     end;
  154.     {Return the index}
  155.     AddString := ITNP^.itnIndex;
  156.   end;
  157.  
  158.   procedure UniqueStringArray.RemoveString(Which : Word);
  159.     {-Remove specified string from array and pack character table}
  160.   var
  161.     ITNP : IndexTreeNodePtr;
  162.     Index : Word;
  163.   begin
  164.     ITNP :=IndexTreeNodePtr(usTP^.Find(GetStringPtr(Which)));
  165.     if ITNP <> nil then begin
  166.       usTP^.Remove(ITNP);
  167.       Dispose(ITNP, Done);
  168.     end;
  169.     StringArray.RemoveString(Which);
  170.   end;
  171.  
  172.   procedure UniqueStringArray.RemoveStringByName(St : String);
  173.     {-Remove named string}
  174.   var
  175.     ITNP : IndexTreeNodePtr;
  176.     Index : Word;
  177.   begin
  178.     ITNP :=IndexTreeNodePtr(usTP^.Find(@St));
  179.     if ITNP <> nil then begin
  180.       Index := ITNP^.itnIndex;
  181.       usTP^.Remove(ITNP);
  182.       Dispose(ITNP, Done);
  183.       StringArray.RemoveString(Index);
  184.     end;
  185.   end;
  186.  
  187.   procedure UniqueStringArray.Clear;
  188.     {-Remove all strings from array}
  189.   begin
  190.     StringArray.Clear;
  191.     usTP^.Clear;
  192.   end;
  193.  
  194.   function UniqueStringArray.GetTreePtr : IndexTreePtr;
  195.     {-Return address of associated IndexTree}
  196.   begin
  197.     GetTreePtr := usTP;
  198.   end;
  199.  
  200. {$IFDEF UseStreams}
  201.   constructor UniqueStringArray.Load(var S : IdStream);
  202.     {-Load a binary packed array from a stream. NOT SUPPORTED}
  203.   begin
  204.     Fail;
  205.   end;
  206.  
  207.   procedure UniqueStringArray.Store(var S : IdStream);
  208.     {-Write a packed array to a stream. NOT SUPPORTED}
  209.   begin
  210.   end;
  211. {$ENDIF}
  212.  
  213. end.
  214.